var common=require('../../utils/cityArray.js');
var array = common.getAreaInfo();//在头部引入 省市区地址js,这里封装成了方法了。
var arrays = common.getAreaInfo1();//在头部引入 省市区地址js,这里封装成了方法了。
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item:String
  },
 lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {

     console.log(array)
    },
    moved: function () { 
    console.log(2) 
    },
    detached: function () { 
      console.log(3) 
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    citysIndex: [0, 0], //给一个初始值索引，因为有三列，所以3个0
    cityArray:array,
    city:'武汉',
    provience:'湖北省'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    func_changeCitysChange: function(e) {
    var that = this;
    var cityArray = that.data.cityArray;
    
    var address='';
    if (that.data.ssq == undefined){
      //下面方法中没有设置ssq，应该给它默认值 ，此时citysIndex相当于[0,0,0]
      var citysIndex = that.data.citysIndex;
      for (let i in citysIndex) {

        address = cityArray[i][citysIndex[i]]
      }
    }else{
      address = that.data.ssq;
    }
    
    var _self=this;
     _self.setData({
            item:address
          })
     _self.triggerEvent('myevent', _self.data.provience+','+address);
     return
    wx.showModal({
      title: '',
      content: '当前选择'+address+'',
      success (res) {
        if (res.confirm) {
          _self.setData({
            item:address
          })
          console.log('存值'+address+_self.data.provience)
           _self.triggerEvent('myevent', _self.data.provience+','+address)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  func_changeCitysChangeColumn: function(e) {
    var that = this;
    var cityArray = that.data.cityArray;
     console.log(e)
    var list1 = []; //存放第二列数据，即市的列
    // var list2 = []; //存放第三列数据，即区的列
    
    var citysIndex = [];
    //主要是注意地址文件中的字段关系，省、市、区关联的字段有 sheng、di、level
    switch (e.detail.column) {
      case 0:
        var sheng;
        //滑动左列
        for (let i = 0, len = arrays.length; i < len; i++) {          
          if (arrays[i]['name'] == cityArray[0][e.detail.value]) {
            console.log(cityArray[0][e.detail.value])
            that.setData({
              provience:cityArray[0][e.detail.value]
            })
            sheng = arrays[i]['sheng'];
          }
          if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 2) {
            list1.push(arrays[i]['name']);
          }
          // if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 3 && arrays[i]['di'] == arrays[1]['di']) {
          //   list2.push(arrays[i]['name']);
          // }
        }       
        

        citysIndex = [e.detail.value, 0, 0];
        var ssq =  list1[0];          
       console.log("sheng="+sheng)
        that.setData({
          global_sheng: sheng
        });  
        break;
      case 1:
        //滑动中列
        var  di;
        var sheng = that.data.global_sheng;
        list1 = cityArray[1];
        for (let i = 0, len = arrays.length; i < len; i++) {     
          if (arrays[i]['name'] == cityArray[1][e.detail.value]) {
            di = arrays[i]['di'];
          }         
        } 
        // for (let i = 0, len = arrays.length; i < len; i++) {
        //   if (arrays[i]['sheng'] == sheng && arrays[i]['level'] == 3 && arrays[i]['di'] == di) {
        //     list2.push(arrays[i]['name']);
        //   }
        // }
        citysIndex = [that.data.citysIndex[0], e.detail.value, 0];
        
        var ssq =  list1[e.detail.value]  + '';

        break;
      case 2:
        // //滑动右列
        // list1 = cityArray[1];
        // list2 = cityArray[2];
        // citysIndex = [that.data.citysIndex[0], that.data.citysIndex[1], e.detail.value];

        // var ssq = cityArray[0][that.data.citysIndex[0]] + list1[that.data.citysIndex[1]] + list2[e.detail.value] + '';
        // break;
    }

    that.setData({
      "cityArray[1]": list1,//重新赋值中列数组，即联动了市
      // "cityArray[2]": list2,//重新赋值右列数组，即联动了区
      citysIndex: citysIndex,//更新索引
      ssq: ssq,//获取选中的省市区
    });
  },

  }
})
